const app = getApp();
var util = require('../../utils/util.js');
Page({
    data: {
        imgUrl:app.globalData.APIURL + '/img',
        lastCycle: '',
        sales: '',
        profit: '',
        lastMonth: new Date().getMonth(),
        dateType: 0,
        filterCurrent:0,
        orderBy: 0,
        sortText:'正序',
        sortImg:'../../images/sort1.png',
        monthSales: [],
        userInfo:{},
        warningBool:null

    },
    deleteWaring:function(){
        this.setData({
            warningBool:false
        })
        app.globalData.warningBool=false;
    },
    onShow:function(){
        this.setData({
            userInfo:wx.getStorageSync('userInfo'),
            warningBool:app.globalData.warningBool
        })
    },
    onLoad: function () {


        wx.setNavigationBarTitle({
            title: '乐购运营人'
        })
        this.getSalesVolume(this.data.dateType);
        this.getLastWeek();
        this.getMonthSales(this.data.orderBy);
    },
    onPullDownRefresh: function () {
        //下拉刷新 获取列表，重置当前页码
        this.getSalesVolume(this.data.dateType);
        this.getMonthSales(this.data.orderBy);
    },

    getDayBetweenMonday: function () {
        var today = new Date();
        //得到今天的星期数(0-6),星期日为0
        var weekday = today.getDay();
        //周日
        if (weekday == 0) {
            return 6;
        } else {
            return weekday - 1;
        }
    },
    getLastDay: function () {
        var today = new Date();
        var dayMSec = 24 * 3600 * 1000;
        var yestodayMSec = today.getTime() - dayMSec;
        var yestoday = new Date(yestodayMSec);
    },
    getLastWeek: function () {
        var dayMSec = 24 * 3600 * 1000;
        var today = new Date();
        //得到距离本周一的天数
        var weekdayBetween = this.getDayBetweenMonday();
        //得到本周星期一的毫秒值
        var nowMondayMSec = today.getTime() - weekdayBetween * dayMSec;
        //得到上周一的毫秒值
        var lastMondayMSec = nowMondayMSec - 7 * dayMSec;
        // 得到上周日的毫秒值
        var lastSundayMSec = nowMondayMSec - 1 * dayMSec;
        var lastMonday = new Date(lastMondayMSec);
        var lastSunday = new Date(lastSundayMSec);
        this.setData({
            lastCycle:util.formatTime(lastMonday).slice(5,10) + '-' + util.formatTime(lastSunday).slice(5,10)
        })
    },
    filterClick: function (e) {

        var type = e.currentTarget.id;

        this.setData({
            filterCurrent: type
        })
        console.log(this.data.filterCurrent)
        this.getSalesVolume(type);
    }
    ,
    getSalesVolume: function (type) {
        if (!type) {
            type = this.data.dateType;
        }
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/homepage/getSalesIncome',
            data: {
                token: app.globalData.token,
                type: type,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                // 停止刷新
                wx.stopPullDownRefresh();
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        sales: res.data.sales,
                        profit: res.data.income,
                    })
                }

            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    },
    ordersManage: function () {
        wx.navigateTo({
            url: '/pages/orders/orders'
        })
    },
    sort: function (){
        if (this.data.orderBy==0) {
            this.getMonthSales(1);
            this.setData({
                orderBy:1,
                sortImg:'../../images/sort2.png',
                sortText:'倒序'
            })
        } else {
            this.getMonthSales(0);
            this.setData({
                orderBy: 0,
                sortImg:'../../images/sort1.png',
                sortText:'正序'
            })
        }

    },
    getMonthSales: function (orderBy) {
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/homepage/getMonthSales',
            data: {
                token: app.globalData.token,
                order_by: orderBy,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                // 停止刷新
                wx.stopPullDownRefresh();
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        monthSales: res.data.salesList
                    })
                }

            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    }


})
;